package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.OrderInfo;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.ViewFindUtils;

import java.util.List;

/**
 * Created by yang on 15/12/21.
 */
public class GoodsJudgeListAdapter extends BaseAdapter {
    private Context mContext;
    private List<OrderInfo.MyOrderItem.ExtendOrderGoods> data;
    public GoodsJudgeListAdapter(Context context, List<OrderInfo.MyOrderItem.ExtendOrderGoods> data){
        mContext = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_goods_judge_list_view,parent,false);
        }
        ImageView img_head = ViewFindUtils.hold(convertView,R.id.store_item_icon);
        TextView tv_name = ViewFindUtils.hold(convertView,R.id.store_item_title);
        TextView tv_price = ViewFindUtils.hold(convertView,R.id.store_item_price);
        TextView tv_num = ViewFindUtils.hold(convertView,R.id.store_item_num);
        RatingBar rb_judge = ViewFindUtils.hold(convertView,R.id.evaluate_sudu1);
        EditText et_content = ViewFindUtils.hold(convertView,R.id.judge_content);

        final EditText et_copy = et_content;

        OrderInfo.MyOrderItem.ExtendOrderGoods eog = data.get(position);
        MyTool.showPicture(mContext,img_head,eog.getGoods_image_url());
        tv_name.setText(eog.getGoods_name());
        tv_price.setText("¥ " + eog.getGoods_price() + "元");
        tv_num.setText("X " + eog.getGoods_num());
        rb_judge.setTag(position);
        et_content.setTag(position);
        rb_judge.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                int tag = (int) ratingBar.getTag();
                OrderInfo.MyOrderItem.ExtendOrderGoods extendOrderGoods = data.get(tag);
                extendOrderGoods.setScore((int)rating + "");
            }
        });

        et_content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                int tag = (int) v.getTag();
                OrderInfo.MyOrderItem.ExtendOrderGoods extendOrderGoods = data.get(tag);
                extendOrderGoods.setContent(et_copy.getText().toString().trim());
            }
        });


        return convertView;
    }
}
